package org.gdpu.model;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-6
 * Time: 下午7:25
 * To change this template use File | Settings | File Templates.
 */

import org.gdpu.util.ChangeTime;
import org.gdpu.util.ExcelResources;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 试题
 */
@Entity
public class Test {

    /**
     * 试题id
     */
    private int id;
    /**
     * 试题编号
     */
    private String testId;
    /**
     * 试题科目
     */
    private Subject subject;

    /**
     * 试题题目(选择题例子：题目（）。A**B**C**D**E*可以不用固定选项)
     */
    private String theme;

    /**
     * 试题答案
     */
    private TestAnswer answer;

    /**
     * 题型
     */
    private TestCategory category;

    /**
     * 难度
     */
    private double difficulty;
    /**
     * 上传者
     */
    private String owner;
    /**
     * 关键字
     */
    private String keyWord;
    /**
     * 1 为共享
     * 0 为私有
     */
    private int state;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "subjectId")
    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    @Column(nullable = false, length = 4000)
    @ExcelResources(title = "试题题目", order = 1)
    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    @ManyToOne
    @ExcelResources(title = "试题答案", order = 2)
    @JoinColumn(name = "answerId")
    public TestAnswer getAnswer() {
        return answer;
    }

    public void setAnswer(TestAnswer answer) {
        this.answer = answer;
    }

    @ManyToOne
    @JoinColumn(name = "categoryId")
    @ExcelResources(title = "试题题型", order = 3)
    public TestCategory getCategory() {
        return category;
    }

    public void setCategory(TestCategory category) {
        this.category = category;
    }

    public double getDifficulty() {
        return difficulty;
    }

    public void setDifficulty(double difficulty) {
        this.difficulty = difficulty;
    }

    public String getTestId() {
        return testId;
    }

    public void setTestId(String testId) {
        this.testId = testId;
    }



    @Column(length = 4000)
    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public Test(int id, String theme, double difficulty, TestAnswer answer, TestCategory category, Subject subject) {
        super();
        this.testId = ChangeTime.formatDate(ChangeTime.getCurrentDate());
        this.id = id;
        this.difficulty = difficulty;
        this.theme = theme;
        this.answer = answer;
        this.category = category;
        this.subject = subject;
    }

    public Test() {
        super();
    }

    @Column(columnDefinition = "INT default 1")
    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    /**
     * 转换选择题格式
     * @param choiceTheme 原格式字符串
     * @return 转换后字符串
     */
    public static String transformChoice(String choiceTheme) throws Exception{
        StringBuffer transform = new StringBuffer();
        int flag;
        int i = 65;    //A字符
        List<Integer> indexs = new ArrayList<Integer>();
        choiceTheme = choiceTheme.replace("(", "（");
        choiceTheme = choiceTheme.replace(")", "）");
        choiceTheme = choiceTheme.replaceAll("[\\t\\n\\r\\s\\f]", "");
        if (choiceTheme.indexOf("（" + (char) i + "）") > 0) {
            while (choiceTheme.indexOf((char) i + "）") > 0) {
                indexs.add(choiceTheme.indexOf((char) i + "）"));
                ++i;
            }
            flag = 1;    //格式1
        } else {
            while (choiceTheme.indexOf((char) i + "）") > 0) {
                indexs.add(choiceTheme.indexOf((char) i + "）"));
                ++i;
            }
            while (choiceTheme.indexOf((char) i + "、") > 0) {
                indexs.add(choiceTheme.indexOf((char) i + "、"));
                ++i;
            }

            while (choiceTheme.indexOf((char) i + ".") > 0) {
                indexs.add(choiceTheme.indexOf((char) i + "."));
                ++i;
            }
            flag = 2;    //格式2
        }

        String input[] = new String[indexs.size() + 1];
        if (flag == 1) {
            input[0] = choiceTheme.substring(0, indexs.get(0) - 1);
        }
        if (flag == 2) {
            input[0] = choiceTheme.substring(0, indexs.get(0));
        }
        input[0] = "<p>" + input[0] + "</p>";
        transform.append(input[0]);
        String themeIndex;
        for (i = 1; i < indexs.size() + 1; i++) {
            if (i == indexs.size())
                themeIndex = choiceTheme.substring(indexs.get(i - 1) + 2);
            else
                themeIndex = choiceTheme.substring(indexs.get(i - 1) + 2, indexs.get(i));
            input[i] = "<div><span><strong>" + (char) (i + 64) + ".</strong></span><div>" + themeIndex + "</div></div>";
            transform.append(input[i]);
        }

        return transform.toString();

    }


}
